import 'dart:io';

import 'package:aif_photo_plugin/aif_photo_plugin.dart';
import 'package:aif_photo_plugin/photo_models/asset_model.dart';
import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  String _imagePath;
  String _videoPath = "https://img.asman.com.cn/a487bfa3dedb4b2581bb9d7bc8126ea5.mp4";
  VideoPlayerController _controller;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          TextButton(
              onPressed: getAllAlbums,
              child: Center(
                child: Text(
                  "all album",
                  style: TextStyle(
                    color: Colors.amber,
                    fontSize: 14,
                  ),
                ),
              )
          ),
          image(),
          video(),
        ],
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  getAllAlbums() async {
    // final allAlbums = await AifPhotoPlugin.getSmartAlbums();
    // allAlbums.forEach((element) {
    //   print("**********");
    //   print(element.localIdentifier);
    //   print(element.localizedTitle);
    // });
    // return;
    // me : 6A430921-1B4A-45C4-AFB6-F138228CFA9C/L0/040
    // 648B9B8E-9A42-44D1-8C4F-919FD4C38D66/L0/040
    final videoAssets = await AifPhotoPlugin.getAlbumAssets(
        "648B9B8E-9A42-44D1-8C4F-919FD4C38D66/L0/040");

    final firstAsset = videoAssets[7];
    if (firstAsset != null) {
      final path = await AifPhotoPlugin.getAssetPath(firstAsset.id);
      final thumbPath = await AifPhotoPlugin.getAssetThumbPath(firstAsset.id);
      print(firstAsset.type);
      print(firstAsset.id);
      print(path);
      switch (firstAsset.type) {
        case AIMediaType.image:
          {
            setState(() {
              _imagePath = "/var/mobile/Media/DCIM/103APPLE/IMG_3316.PNG";
            });
            break;
          }
        case AIMediaType.video:
          {
            setState(() {
              _videoPath =
              "/var/mobile/Media/PhotoData/CPLAssets/group75/C3EF9393-9711-4637-8DBC-2C220E333CC2.MP4";
              _imagePath = thumbPath;
            });
            break;
          }
        default:
          break;
      }
    }
  }

  Widget image() {
    if (_imagePath != null && _imagePath.length >= 0) {
      final file = File(_imagePath);

      return Image.file(file, width: 200, height: 100,);
    }
    return Container();
  }

  Widget video() {
    if (_videoPath == null || _videoPath.length <= 0) {
      return Container();
    }
    final file = File(_videoPath);
    // print(file);
    final networkPath = "https://img.asman.com.cn/a487bfa3dedb4b2581bb9d7bc8126ea5.mp4";
    print(networkPath);
    _controller = VideoPlayerController.file(file)
        ..initialize();
    print(_controller);
    print("display video");
    final kScreenWidth = MediaQuery
        .of(context)
        .size
        .width;
    return Container(
      width: kScreenWidth,
      height: kScreenWidth,
      color: Colors.amber,
      child: Stack(
        children: [
          Container(
            width: kScreenWidth,
            height: kScreenWidth,
            color: Colors.blue,
            child: VideoPlayer(
              _controller,
            ),
          ),
          Center(
            child: IconButton(
              onPressed: (){
                _controller.value.isPlaying
                    ? _controller.pause()
                    : _controller.play();
              },
              icon: Icon(
                _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
                size: 60,
                color: Colors.white,
              ),
            ),
          )
        ],
      ),
    );
  }
}
